package android.medicalcloud.com.yiapp.yilian.im.adapter;

import android.content.Context;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.adapter.BasePlatAdapter;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.im.model.FriendProfile;
import android.medicalcloud.com.yiapp.yilian.im.model.FriendshipInfo;
import android.medicalcloud.com.yiapp.yilian.im.model.Message;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.utils.android.ImageLoader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.TIMUserProfile;

import java.util.Properties;


/**
 * 聊天界面adapter
 */
public class ChatAdapter extends BasePlatAdapter<Message> {

    private final String TAG = "ChatAdapter";


    private View view;
    private ViewHolder viewHolder;

    public ChatAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.im_item_message, null);
            viewHolder = new ViewHolder();
            viewHolder.leftMessage = (RelativeLayout) view.findViewById(R.id.leftMessage);
            viewHolder.rightMessage = (RelativeLayout) view.findViewById(R.id.rightMessage);
            viewHolder.leftPanel = (RelativeLayout) view.findViewById(R.id.leftPanel);
            viewHolder.rightPanel = (RelativeLayout) view.findViewById(R.id.rightPanel);
            viewHolder.sending = (ProgressBar) view.findViewById(R.id.sending);
            viewHolder.error = (ImageView) view.findViewById(R.id.sendError);
            viewHolder.sender = (TextView) view.findViewById(R.id.sender);
            viewHolder.rightDesc = (TextView) view.findViewById(R.id.rightDesc);
            viewHolder.leftAvatar = (ImageView) view.findViewById(R.id.leftAvatar);
            viewHolder.rightAvatar = (ImageView) view.findViewById(R.id.rightAvatar);
            viewHolder.systemMessage = (TextView) view.findViewById(R.id.systemMessage);
            view.setTag(viewHolder);
        }
        if (position < getCount()) {
            final Message data = getItem(position);
            try {
                FriendProfile profile = FriendshipInfo.getInstance().getProfile(data.getMessage().getSender());
//                TIMUserProfile profile = data.getMessage().getSenderProfile();
                Logger.e("--ww-----"+data.isSelf());
                Logger.e("--ww-----"+profile);
                if (data.isSelf()) {
                    ImageLoader.getInstance(context).displayCileImage(UserController.getInstance().getImageAvar(), viewHolder.rightAvatar);
                } else {
                    Logger.e("--ww-----"+profile.getAvatarUrl());
                    ImageLoader.getInstance(context).displayCileImage(profile.getAvatarUrl(), viewHolder.leftAvatar);
                }
            } catch (Exception e) {

            }
            data.showMessage(viewHolder, context);
        }
        return view;
    }


    public class ViewHolder {
        public RelativeLayout leftMessage;
        public RelativeLayout rightMessage;
        public RelativeLayout leftPanel;
        public RelativeLayout rightPanel;
        public ProgressBar sending;
        public ImageView error;
        public TextView sender;
        public TextView systemMessage;
        public TextView rightDesc;
        public ImageView leftAvatar, rightAvatar;
    }
}
